import unittest, requests
from time import sleep

from api.api_register_login import ApiRegisterLogin
from util import common_assert, read_json, clear_data, build_loan_data
from parameterized import parameterized


class TestRegisterLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        # 清除会员数据
        clear_data()
        # 构造借款业务数据
        # build_loan_data()

    # 初始化
    def setUp(self) -> None:
        # 获取session对象
        self.session = requests.session()
        # 获取ApiRegisterLogin对象
        self.reg = ApiRegisterLogin(self.session)

    # 结束
    def tearDown(self) -> None:
        # 关闭session
        self.session.close()

    # 1、获取图片验证码接口 测试
    @parameterized.expand(read_json("register_login.json", "img_code"))
    def test01_img_code(self, random, expect_code):
        try:
            result = self.reg.api_img_code(random)
            # 断言
            common_assert(self, result, expect_code=expect_code)
        except Exception as e:
            # 日志

            # 抛异常
            raise

    # 2、获取短信验证码接口 测试
    @parameterized.expand(read_json("register_login.json", "phone_code"))
    def test02_phone_code(self, phone, imgVerifyCode, expect_text):
        try:
            # 1、调用图片验证码接口
            self.reg.api_img_code(123)
            # 2、调用短信验证码接口
            result = self.reg.api_phone_code(phone, imgVerifyCode)
            common_assert(self, result, expect_text=expect_text)
        except Exception as e:
            # 日志

            # 抛异常
            raise

    # 3、注册接口 测试
    @parameterized.expand(read_json("register_login.json", "register"))
    def test03_register(self, phone, password, imgVerifyCode, phone_code, expect_text):
        try:
            # 1、调用图片验证码接口
            self.reg.api_img_code(123)
            # 2、调用短信验证码接口
            self.reg.api_phone_code(phone, imgVerifyCode)
            # 3、调用注册接口
            result = self.reg.api_register(phone=phone, password=password, verifycode=imgVerifyCode,
                                           phone_code=phone_code)
            common_assert(self, result, expect_text=expect_text)
        except Exception as e:
            # 日志

            # 抛异常
            raise

    # 4、登录接口 测试
    @parameterized.expand(read_json("register_login.json", "login"))
    def test04_login(self, keywords, password, expect_text):
        try:
            if password == "e rror123":
                i = 0
                result = None
                while i < 3:
                    # 调用登录
                    result = self.reg.api_login(keywords=keywords, password=password)
                    i += 1
                # 断言锁定
                self.assertIn("锁定", result.text)
                # 暂停60秒
                sleep(60)
                # 调用登录成功
                result = self.reg.api_login(keywords="13600001111", password="test123")
                common_assert(self, result, expect_text=expect_text)
            else:
                result = self.reg.api_login(keywords=keywords, password=password)
                common_assert(self, result, expect_text=expect_text)
        except Exception as e:
            # 日志

            # 抛异常
            raise

    # 5、查询登录状态接口 测试
    @parameterized.expand(read_json("register_login.json", "login_status"))
    def test05_login_status(self, status, expect_text):
        try:
            if status == "已登录":
                # 调用登录方法
                self.reg.api_login(keywords="13600001111", password="test123")
            # 调用查询登录状态方法
            result = self.reg.api_login_status()
            # 断言结果
            common_assert(self, result, expect_text=expect_text)
        except Exception as e:
            # 日志

            # 抛异常
            raise
